// 太阳能发电系统监控界面 - 天气页面脚本

// 初始化Vue应用
const vm = new Vue({
    el: '#app',
    data: {
      // 从全局数据获取
      lastUpdateTime: appData.lastUpdateTime,
      showAlert: appData.showAlert,
      alertMessage: appData.alertMessage,
      weatherData: { ...appData.weatherData },
      
      // 天气页面特定数据
      currentDate: '',
      dayOfWeek: '',
      weatherDesc: '',
      
      // 刷新计时器
      refreshInterval: null
    },
    computed: {
      // 天气图标类
      weatherIconClass() {
        return appMethods.getWeatherIconClass(this.weatherData.weatherCondition);
      },
      
      // 天气描述文字
      weatherDescription() {
        switch(this.weatherData.weatherCondition) {
          case 'sunny': return '晴天';
          case 'cloudy': return '多云';
          case 'rainy': return '雨天';
          case 'stormy': return '雷暴';
          default: return '晴天';
        }
      },
      
      // 降雨概率状态
      rainChanceStatus() {
        if (this.weatherData.rainChance < 20) return '低';
        if (this.weatherData.rainChance < 50) return '中';
        return '高';
      }
    },
    methods: {
      // 更新最后更新时间
      updateLastUpdateTime() {
        this.lastUpdateTime = appMethods.updateLastUpdateTime();
      },
      
      // 更新当前日期信息
      updateDateInfo() {
        const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
        const now = new Date();
        
        this.dayOfWeek = days[now.getDay()];
        this.currentDate = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日`;
      },
      
      // 刷新天气数据
      refreshWeather() {
        // 调用共享方法
        appMethods.refreshWeather();
        
        // 模拟网络延迟后同步数据
        setTimeout(() => {
          this.weatherData = { ...appData.weatherData };
          this.lastUpdateTime = appData.lastUpdateTime;
        }, 600);
      },
      
      // 显示系统报警
      showSystemAlert(message) {
        appMethods.showSystemAlert(message);
        this.showAlert = appData.showAlert;
        this.alertMessage = appData.alertMessage;
      },
      
      // 确认报警
      acknowledgeAlert() {
        appMethods.acknowledgeAlert();
        this.showAlert = appData.showAlert;
      },
      
      // 获取预报项图标
      getForecastIcon(condition) {
        return appMethods.getWeatherIconClass(condition);
      },
      
      // 启动自动刷新
      startAutoRefresh() {
        // 每2分钟刷新一次天气数据
        this.refreshInterval = setInterval(() => {
          this.refreshWeather();
        }, 120000);
      },
      
      // 停止自动刷新
      stopAutoRefresh() {
        if (this.refreshInterval) {
          clearInterval(this.refreshInterval);
          this.refreshInterval = null;
        }
      }
    },
    mounted() {
      // 确保从localStorage加载最新设置
      appMethods.loadSettingsFromStorage();
      
      // 更新日期信息
      this.updateDateInfo();
      
      // 更新初始时间
      this.updateLastUpdateTime();
      
      // 启动自动刷新
      if (appData.settingsData.autoRefresh) {
        this.startAutoRefresh();
      }
    },
    beforeDestroy() {
      // 清理定时器
      this.stopAutoRefresh();
    }
  });